<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="./js/calendar.js"></script>
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript">

	function checkForm(){
		var contractCode = document.getElementById("contractCode");
		var contractName = document.getElementById("contractName");
		var customerName = document.getElementById("customerName");
		var contractDate = document.getElementById("contractDate");
		var salePerson = document.getElementById("salePerson");
		
		var purchaseDescs = document.getElementsByName("purchaseDesc");
		var nums = document.getElementsByName("num");
		var costValues = document.getElementsByName("costValue");
		var discounts = document.getElementsByName("discount");
		var costss = document.getElementsByName("costs");
		var prossProfits = document.getElementsByName("prossProfit");
		
		var currentDate = document.getElementById("currentDate");

		return true;
	}

	function back(){
		window.location.href = "./contract.do";
		window.location.target = "main";
	}
	
	function customerDialog(){
		var url = "./contract.do?action=selectCustomer";
		var dlgParam = "dialogHeight:360px;dialogWidth:430px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取客户id和客户名称
			var customerId = "";
			var customerName = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				customerId = tStr[0];
				customerName = tStr[1];
			}
			document.getElementById("customerId").value = customerId;										
			document.getElementById("customerName").value = customerName;
			document.getElementById("contractName").value = customerName + "销售合同";
		}
	}
	
	function caseTypeDialog(){
		var url = "./contract.do?action=selectCaseType";
		var dlgParam = "dialogHeight:360px;dialogWidth:430px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取客户id和客户名称
			var typeId = "";
			var caseType = "";
			var caseCode = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				typeId = tStr[0];
				caseType = tStr[1];
				caseCode = tStr[2];
			}
			document.getElementById("typeId").value = typeId;										
			document.getElementById("caseType").value = caseType;
			document.getElementById("caseCode").value = caseCode;
		}
	}	
	
	function processDialog(indexFlag, groupId){
		var url = "./contract.do?action=selectProcessDetail&indexFlag="+indexFlag+"&groupId="+groupId;
		var dlgParam = "dialogHeight:360px;dialogWidth:430px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取客户id和客户名称
			var processGroupId = "";
			var processId = "";
			var processName = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				processGroupId = tStr[0];
				processId = tStr[1];
				processName = tStr[2];
			}
			document.getElementById("processGroupId").value = processGroupId;										
			document.getElementById("processId").value = processId;
			document.getElementById("processName").value = processName;
		}
	}	
	
	function productDialog(){
		var url = "selectProduct.html";
		var dlgParam = "dialogHeight:400px;dialogWidth:500px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		window.showModalDialog(url,"",dlgParam);
	}
	
	function save(){	
		if(checkForm()){
			document.addContractForm.action = "./contract.do?action=updateContractCase";
			document.addContractForm.target = "main";
			document.addContractForm.submit();
			document.getElementById("saveBtn").href = "#";
		}
	}
	
	function getCostsMain(obj,rowNum){
		onlyNumFloat(obj);
		getCosts(rowNum);
	}
	//计算成本
	function getCosts(rowNum){
		rowNum = Number(rowNum) - 1;
		var costValues = document.getElementsByName("costValue");//折扣后的金额
		//var discounts = document.getElementsByName("discount");//折扣率
		var costss = document.getElementsByName("costs");//成本
		var prossProfits = document.getElementsByName("prossProfit");//毛利
		if(costValues.length > 0 && 
			costss.length > 0 && prossProfits.length > 0){
			if(costValues[rowNum].value != ""){
				document.getElementsByName("costs")[rowNum].value = 
					Number(costValues[rowNum].value) - Number(prossProfits[rowNum].value);
			}
		}
	}
	
	function getProssProfitMain(obj,rowNum){
		onlyNumFloat(obj);
		getProssProfit(rowNum);
	}
	//计算毛利
	function getProssProfit(rowNum){
		rowNum = Number(rowNum) - 1;
		var costValues = document.getElementsByName("costValue");//折扣后的金额
		//var discounts = document.getElementsByName("discount");//折扣率
		var costss = document.getElementsByName("costs");//成本
		var prossProfits = document.getElementsByName("prossProfit");//毛利
		if(costValues.length > 0  
			&& costss.length > 0 && prossProfits.length > 0){
			if(costValues[rowNum].value != ""){
				document.getElementsByName("prossProfit")[rowNum].value = 
					Number(costValues[rowNum].value) - Number(costss[rowNum].value);
			}
		}
	}
	
	function getCostsAndProssProfitMain(rowNum){
		getCosts(rowNum);
		getProssProfit(rowNum);
	}
	function getCostsAndProssProfit(obj,rowNum){
		onlyNumFloat(obj);
		getCosts(rowNum);
		getProssProfit(rowNum);
	}
	
	function personDialog(){
		var url = "./contract.do?action=selectSalePerson";
		var dlgParam = "dialogHeight:350px;dialogWidth:400px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取销售代表id和销售代表名称
			var salePersonId = "";
			var salePerson = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				salePersonId = tStr[0];
				salePerson = tStr[1];
			}
			document.getElementById("salePersonId").value = salePersonId;										
			document.getElementById("salePerson").value = salePerson;
		}
	}
	
	//追加行start
	function addRow(tableId){//增加tab的一行	
		try{				
			var objT =  document.getElementById(tableId);//通过ID 得到对象
			if(objT==null){	alert("没有可操作的表！");return;	}
			if(objT.rows.length<1){ alert("没有表格标题行"); return;}
			
			var newTr = objT.insertRow(-1);//插入一行
			newTr.className = "table_style";	//为新行赋样式
			var colsLength = objT.rows(0).cells.length;  //表中列总数 
			var newTd ;
			var hiddenObj;		
			var insertTdHtml = prepareInsertHtml(newTrNum);//获得新增行各个TD的HTML代码
			var i=0;
			for(i=0;i<colsLength;i++){
				newTd = newTr.insertCell();
				newTd.innerHTML = insertTdHtml[i];
				newTd.align="left";				
			}
			newTrNum++;//表格总行数加1
		}catch(Exception){}
	}
	
	//增加行所需代码
	var newTrNum = 0;//表格最大行号，只增不减
	var insertHtml = new Array();//用于插入新记录的HTML
	function prepareInsertHtml(num){
		insertHtml[0] = "<input id='purchaseDesc' name='purchaseDesc' class='input_percent' type='text'/>";
		insertHtml[0] += "<input id='unit' name='unit' type='hidden'/>";	
		insertHtml[0] += "<input id='price' name='price' type='hidden'/>";
		//insertHtml[1] = "<input id='unit' name='unit' type='hidden'/>";	
		//insertHtml[2] = "<input id='price' name='price' type='hidden'/>";	
		insertHtml[1] = "<input id='num' name='num' class='input_percent' type='text' onkeyup='onlyNumber(this)'/>";	
		insertHtml[2] = "<input id='discount' name='discount' class='input_percent' type='text' value='100' onkeyup='getCostsAndProssProfit(this,"+num+")' onblur='getCostsAndProssProfitMain("+num+")'/>";
		insertHtml[3] = "<input id='costValue' name='costValue' class='input_percent' type='text' onkeyup='getCostsAndProssProfit(this,"+num+")' onblur='getCostsAndProssProfitMain("+num+")'/>";
		insertHtml[4] = "<input id='costs' name='costs' class='input_percent' type='text' onkeyup='getProssProfitMain(this,"+num+")' onblur='getProssProfit("+num+")'/>";
		insertHtml[5] = "<input id='prossProfit' name='prossProfit' class='input_percent' type='text' onkeyup='getCostsMain(this,"+num+")' onblur='getCosts("+num+")'/>";
		//insertHtml[6] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img class='btn_img' src='./images/a_add.gif' onclick='productDialog()' title='选择产品'>";
		return insertHtml;
	}
	//追加行end
	
	function initPage(){
		newTrNum = table1.rows.length;//表格最大行号，只增不减
		
		if("<c:out value='${result}'/>" == 1){
			alert("<fmt:message key='common.message.addSuccess'/>");
		}
		if("<c:out value='${failResult}'/>" == 3){
			alert("保存失败：上传文件大小不能超过10M!");
		}
		if("<c:out value='${repeatResult}'/>" == 1){
			alert("保存失败：合同编码已存!");
		}
		
		document.getElementById("saveBtn").href = "javascript:save();";
	}
</script>
</head>
<body onload="initPage()">
<form name="addContractForm" action="" method="post" enctype="multipart/form-data">
<input id="contractCaseId" name="contractCaseId" type="hidden" value="${contractCase.id}"/>

	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab">案件详情</td>
					</tr>
					<tr>
						<td align="center" valign="bottom">
							<button class="btn_submit" onclick="save()" hidefocus="true">提交</button>&nbsp;&nbsp;
							<button class="btn_back" onclick="back()" hidefocus="true">取消并返回</button>
						</td>
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
						<table class="table_add">
								<tr><td colspan="4" class="title_top">案件信息</td></tr>
								<tr>
									<td class="title">案件类型：</td>
									<td class="">
										<input id="caseType" name="caseType" class="input_long" type="text" value="${contractCase.caseTypeName}" readonly="readonly"/>
										<input id="typeId" name="typeId" type="hidden" value="${contractCase.contractCaseType.id}"/>
										<input class="btn_select" type="button" value="" onclick="caseTypeDialog()" hidefocus="true"/>
									</td>
									<td class="title">案件阶段：</td>
									<td class="">
										<input id="processGroupId" name="processGroupId" type="hidden" value="${contractCase.processGroupId}"/>
										<input id="processId" name="processId" type="hidden" value="${contractCase.processId}"/>								
										<input id="processName" name="processName" class="input_long" type="text" value="${contractCase.processName}"/>
										<input class="btn_select" type="button" value="" onclick="processDialog(${contractCase.process.indexFlag}, ${contractCase.processGroupId})" hidefocus="true"/>	
									</td>
								</tr>
								<tr>
									<td class="title">案件编号：</td>
									<td class="">
										<input id="caseCode" name="caseCode" class="input_long" type="text" value="${contractCase.caseCode}"/>
									</td>
									<td class="title">申请号：</td>
									<td class="">
										<input id="applicationCode" name="applicationCode" class="input_long" type="text" value="${contractCase.applicationCode}"/>
									</td>									
								</tr>
								<tr>
									<td class="title">申请日：</td>
									<td class="">
										<input id="applicationDate" name="applicationDate" class="input_long" type="text" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly" value="${contractCase.applicationDate}"/> 									
									</td>
									<td class="title">实用新型名：</td>
									<td class="">
										<input id="description" name="description" class="input_long" type="text" value="${contractCase.description}"/>
									</td>									
								</tr>
								<tr>
									<td class="title">折扣：</td>
									<td class="">
										<input id="discount" name="discount" class="input_long" type="text" value="${contractCase.discount}"/>
									</td>
									<td class="title">业务费用：</td>
									<td class="">
										<input id="operationCost" name="operationCost" class="input_long" type="text" value="${contractCase.operationCost}"/>
									</td>
								</tr>		
								<tr>
									<td class="title">官费：</td>
									<td class="">
										<input id="officialCost" name="officialCost" class="input_long" type="text" value="${contractCase.officialCost}"/>
									</td>
									<td class="title">代理费用：</td>
									<td class="">
										<input id="proxyCost" name="proxyCost" class="input_long" type="text" value="${contractCase.proxyCost}"/>
									</td>
								</tr>																							
							</table>
						</td>
					<tr>
				</table>
			</td>
		</tr>
		
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<c:forEach var="process" items="${processes.processList}">
								${process.name}
								<c:if test="${contractCase.processIndex <= process.indexFlag}">
									---->
								</c:if>
								<c:if test="${contractCase.processIndex > process.indexFlag}">
									----
								</c:if>								
							</c:forEach>
						</td>
					<tr>
				</table>
			</td>
		</tr>		
	</table>
	<input id="currentDate" name="currentDate" type="hidden" value="<fmt:formatDate value='<%=new java.util.Date()%>' pattern="yyyy-MM-dd"/>"/>
</form>	
</body>
</html>